<template>
	<nt-container>
		<view class="detail">
			<view class="nt-submit-item">
				<nt-order-detail :orderInfo="orderInfo"></nt-order-detail>
			</view>
			<template>
				<nt-card label="报价内容">
					<view class="card">
						<view class="card-item" v-for="(it, index) in quote" :key="it.id">
							<view class="card-item__label">{{ it.content }}</view>
							<view class="card-item__right">
								<view class="card-item__price">{{ it.price }}元</view>
							</view>
						</view>
						<view class="card-item">
							<view class="card-item__label">总报价</view>
							<view class="card-item__right amount">{{ orderInfo.order_price }}元</view>
						</view>
					</view>
				</nt-card>

				<nt-card label="总成本" :sub="`${tatalCost}元`">
					<view class="card">
						<view class="card-item">
							<view class="card-item__label">单费</view>
							<view class="card-item__right">
								<view class="card-item__price">{{ orderInfo.cost || 0 }}元</view>
							</view>
						</view>
						<view class="card-item">
							<view class="card-item__label">带会成本</view>
							<view class="card-item__right">
								<view class="card-item__price">{{ orderInfo.cost_back_parts_price }}元</view>
							</view>
						</view>
						<view class="card-item" v-if="invoice">
							<view class="card-item__label">税与邮费</view>
							<view class="card-item__right">
								<view class="card-item__price">{{ invoice.postage }}元</view>
							</view>
						</view>
						<template v-if="companyPartsList.length">
							<view class="card-item" v-for="(it, index) in companyPartsList" :key="it.id">
								<view class="card-item__label">公司配件 -> {{ it.content }}</view>
								<view class="card-item__right">
									<view class="card-item__price">{{ it.price }}元</view>
								</view>
							</view>
						</template>
						<template v-if="personPartsList.length">
							<view class="card-item" v-for="(it, index) in personPartsList" :key="it.id">
								<view class="card-item__label">个人配件 -> {{ it.content }}</view>
								<view class="card-item__right">
									<view class="card-item__price">{{ it.price }}元</view>
								</view>
							</view>
						</template>

						<template v-if="procurePartsList.length">
							<view class="card-item" v-for="(it, index) in procurePartsList" :key="it.id">
								<view class="card-item__label">申购配件 -> {{ it.content }}</view>
								<view class="card-item__right space">
									<view class="card-item__price">{{ it.price }}元</view>
								</view>
							</view>
						</template>
					</view>
				</nt-card>

				<nt-card label="带回配件" v-if="takebackList.length">
					<view class="card-item" v-for="(it, index) in takebackList" :key="it.id">
						<view class="card-item__label">{{ it.takeback_parts_name }} -> {{ it.content }}</view>
						<view class="card-item__right">
							<view class="card-item__price">{{ it.number }} <text
									style="color: #f00;margin: 0 10rpx;">X</text> ￥{{ it.takeback_parts_price }}</view>
						</view>
					</view>
				</nt-card>
				<nt-card label="支付信息">
					<view class="card">
						<template v-for="(it, index) in payment">
							<view class="card-item" :key="it.id">
								<view class="card-item__label">{{ it.paytype }}</view>
								<view class="card-item__right">{{ it.payprice }}元</view>
							</view>
						</template>
						<view class="card-item a-start">
							<view class="card-item__label">支付截图</view>
							<view class="card-item__right">
								<template v-if="orderInfo.payimages">
									<view class="card-item__price">{{ orderInfo.payimages.length }}张</view>
								</template>
							</view>
						</view>
					</view>
				</nt-card>

				<nt-card label="发票" v-if="invoice">
					<view class="card">
						<view class="card-item">
							<view class="card-item__label">发票</view>
							<view class="card-item__right">
								<view class="card-item__remarks"></view>
								<view class="card-item__price">{{ invoice.invoice_content }}</view>
							</view>
						</view>
					</view>
				</nt-card>

				<nt-card label="其他">
					<view class="card">
						<view class="card-item" v-if="orderInfo.warranty_date">
							<view class="card-item__label">保修期</view>
							<view class="card-item__right space">
								<view class="card-item__remarks"></view>
								<view class="card-item__price">{{ orderInfo.warranty_date }}</view>
							</view>
						</view>
						<view class="card-item a-start" v-if="orderInfo.hp_img">
							<view class="card-item__label">好评截图</view>
							<view class="card-item__right space">
								<view class="card-item__remarks"></view>
								<view class="card-item__price">有</view>
							</view>
						</view>
					</view>
				</nt-card>
				<view class="detail-profit">
					利润: <text>{{ orderInfo.profit }}</text>
				</view>

				<view class="detail-footer">
					<template v-if="orderInfo.powerStatus">
						<view class="detail-footer__btn" @tap="showModal = true">完成</view>
						<view class="detail-footer__btn"
							@tap="nav(`/subpackage/order/pages/submit/_id?id=${orderInfo.id}`)">修改</view>
					</template>
				</view>
			</template>
		</view>
		<u-modal :show="showModal" title="温馨提示" content='确认完成该订单吗?' showCancelButton @confirm="completeCurrentOrder"
			@cancel="showModal = false"></u-modal>
	</nt-container>
</template>

<script>
	import {
		getOrderDetail,
		completeOrder,
		getCommonFormConfig
	} from "@/api"
	export default {
		data() {
			return {
				orderId: "",
				orderInfo: {},
				// 报价内容
				quote: [],
				// 公司配件
				companyPartsList: [],
				// 个人配件
				personPartsList: [],
				// 申购配件
				procurePartsList: [],
				// 支付信息
				payment: [],
				// 带回配件
				takebackList: [],
				// 发票信息
				invoice: null,
				showModal: false

			}
		},
		computed: {
			// 总成本
			tatalCost() {
				const {
					cost,
					tax_postage_price,
					cost_back_parts_price,
					order_parts_price
				} = this.orderInfo;
				if (typeof Number(cost) === "number" && typeof Number(tax_postage_price) === "number" && typeof Number(
						cost_back_parts_price) === "number" && typeof Number(order_parts_price) === "number") {
					return Number(cost) + Number(tax_postage_price) + Number(cost_back_parts_price) + Number(
						order_parts_price)
				}
				return 0
			},
		},
		onShow() {
			this.init()
		},
		onLoad(options) {
			this.orderId = options.id
		},
		methods: {
			// 订单详情
			getDetail() {
				getOrderDetail(this.orderId).then(res => {
					const {
						code,
						data,
						msg
					} = res;
					if (code !== 1) return this.toast(msg)
					this.orderInfo = data;
					// 报价内容
					this.quote = data.quote || []
					// 公司配件
					this.companyPartsList = (data.partscompany || []).filter(item => item.is_new === 0)
					// 个人配件
					this.personPartsList = this.personPartsEcho(data.partsperson || [])
					// 申购配件
					this.procurePartsList = (data.partscompany || []).filter(item => item.is_new === 1)
					this.payment = data.payment || []
					this.takebackList = data.takeback || []
					this.invoice = data.invoice || null
				})
			},
			// 获取配件
			getFormConfig(type) {
				return getCommonFormConfig(this.orderId).then(res => {
					console.log(res, '获取配件')
					const {
						code,
						data
					} = res;
					if (code !== 1) return;
					this.formConfig = data
				})
			},
			completeCurrentOrder() {
				completeOrder(this.orderId).then(res => {
					const {
						code,
						data
					} = res;
					if (code === 1) {
						this.toast("订单已完成")
						this.showModal = false
						setTimeout(() => {
							this.back()
							uni.$emit("changeList")
						}, 1000)
					}
				})
			},
			// 处理公司配件和申购配件回显
			personPartsEcho(data) {
				const p = []
				data.map(item => {
					let list = this.formConfig.partsPersonList
					for (let i = 0; i < list.length; i++) {
						if (item.parts_person_id === list[i].value) {
							p.push({
								id: item.id,
								content: list[i].name,
								price: list[i].price,
								number: item.number
							})
						}
					}
				})
				return p
			},
			async init() {
				await this.getFormConfig()
				this.getDetail()
			},
		}
	}
</script>

<style lang="scss" scoped>
	.detail {
		padding: 20rpx 20rpx 160rpx;
		box-sizing: border-box;

		&-title {
			font-size: 30rpx;
			color: #333;
			font-weight: bold;
			height: 90rpx;
			line-height: 90rpx;
			border-bottom: 1rpx solid #e5e5e5;
		}

		/deep/ .nt-card {
			border-radius: 20rpx;
			margin-bottom: 20rpx;

			&-header {
				&__sub {
					margin-left: 20rpx;
					color: #f00;
				}
			}
		}

		&-tip {
			font-size: 30rpx;
			text-align: center;
			line-height: 90rpx;
			color: #666;
		}

		&-profit {
			font-size: 40rpx;
			font-weight: bold;

			text {
				color: #f00;
				margin-left: 20rpx;
			}
		}

		&-footer {
			display: flex;
			align-items: center;
			justify-content: space-between;
			position: fixed;
			bottom: 0;
			left: 0;
			right: 0;
			height: 100rpx;
			padding: 0 20rpx;
			box-sizing: border-box;
			background: #fff;
			box-shadow: 0 0 10rpx rgba(0, 0, 0, 0.2);

			&__btn {
				border-radius: 60rpx;
				border: 1rpx solid var(--theme-color);
				color: var(--theme-color);
				font-size: 24rpx;
				height: 60rpx;
				line-height: 58rpx;
				padding: 0 20rpx;
				min-width: 120rpx;
				text-align: center;
			}
		}
	}

	.card {
		border-radius: 20rpx;
		background: #fff;
		box-sizing: border-box;

		.vertical {
			display: block;
		}

		.end {
			justify-content: flex-end;
		}

		.start {
			justify-content: flex-start;
		}

		.center {
			justify-content: center;
		}

		.space {
			justify-content: space-between;
		}

		.a-end {
			align-items: flex-end;
		}

		.a-start {
			align-items: flex-start;
		}

		.a-center {
			align-items: center;
		}

		.amount {
			font-size: 34rpx;
			font-weight: bold;
			color: #f00
		}

		.subtitle {
			font-size: 28rpx;
			font-weight: bold;
			color: #333;
			height: 80rpx;
			line-height: 80rpx;
			border-bottom: 1rpx solid #e5e5e5;
			width: 100%;
			text-align: left;
			text-align-last: left;
		}

		&.pad {
			padding: 20rpx;
		}

		&-item {
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 10rpx 0;
			font-size: 30rpx;

			&__label {
				// width: 150rpx;
				color: #666;
				font-weight: bold;
				// max-width: 70%;
				width: 500rpx;
			}

			&__right {
				display: flex;
				align-items: center;
				justify-content: flex-end;
				flex: 1;
				flex-shrink: 0;
				color: #333;
				text-align: right;

				&.block {
					display: block;
				}

				&.end {
					justify-content: flex-end;
				}
			}

			&__remarks {
				padding: 0 10rpx;
				box-sizing: border-box;
				max-width: 70%
			}

			&__cell {
				flex: 1;
			}
		}
	}
</style>